import { AbsSvgUnitParser } from "../absunitparser"
import polygon from "../../../../../canvas/common/struct/basestruct/polyline";
import globalconst from "../../../../../canvas/common/globalConst";
var global = globalconst.GlobalConst;

class PolygonParser extends AbsSvgUnitParser{
    constructor(){
        super();
        this.type = global.BaseUnitPolyline<<16;
    }
    parse(mainEle){
        let unitInfo = new polygon.Polyline();
        unitInfo.type = this.type;

        const attributes = mainEle.attributes;
        for (let i = 0; i < attributes.length; i++) {
            const attr = attributes[i];
            if (attr.name == 'points'
                ||attr.name == 'd'
            ) {
                this.parsePoints(attr.value,unitInfo);
            }
        }
        this.parseCommonProp(unitInfo,mainEle);
        unitInfo.strPosition = unitInfo.pos.toString();
        return unitInfo;
    }

    parsePoints(pointStr,unitInfo){
        let newStr = pointStr.replace(/ /g, ",");
        let list = newStr.split(',');
        for (let i = 0;i < list.length;){
            let x = parseFloat(list[i++]);
            let y = parseFloat(list[i++]);
            let pt = {x,y};
            this.chg2SysCoord(pt);
            unitInfo.pos.addPoint(pt);
        }

        let headPt = unitInfo.pos.getHead();
        unitInfo.pos.addPoint(headPt);
    }
}

export {PolygonParser}